package com.paic.mhis.hcpms.workingArea.biz.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.workingArea.biz.service.AuditWorkingAreaService;
import com.paic.mhis.hcpms.workingArea.intergration.dao.AuditWorkingAreaDAO;

/**
 * 
 * @author EX-XIEQI001
 * 
 */
@Service("auditWorkingAreaService")
public class AuditWorkingAreaServiceImpl implements AuditWorkingAreaService {

	@Autowired
	public AuditWorkingAreaDAO auditWorkingAreaDao;

	@Override
	public Map<String, Object> getUnprocessedQuestion(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		map.put("problemStatus", AuditConstants.problemStatus_4);
		int investigatedNum = auditWorkingAreaDao.queryProblemTaskListCount(map);
		map.put("problemStatus", AuditConstants.problemStatus_5);
		int accountsNum = auditWorkingAreaDao.queryProblemTaskListCount(map);

		map.put("caseStatus", AuditConstants.auditCaseStatus_2);
		int auditNum = auditWorkingAreaDao.queryCaseTaskListCount(map);

		map.put("caseStatus", AuditConstants.auditCaseStatus_3);
		int auditReviewNum = auditWorkingAreaDao.queryCaseTaskListCount(map);

		result.put("investigatedNum", investigatedNum);
		result.put("accountsNum", accountsNum);
		result.put("auditNum", auditNum);
		result.put("auditReviewNum", auditReviewNum);

		return result;
	}

	@Override
	public Map<String, Object> getAuditPost(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		map.put("problemStatus", AuditConstants.problemStatus_3);
		int investigatedNum = auditWorkingAreaDao.queryProblemTaskListCount(map);

		map.put("caseStatus", AuditConstants.auditCaseStatus_1);
		int distributionAuditNum = auditWorkingAreaDao.queryCaseTaskListCount(map);

		map.put("problemStatus", AuditConstants.problemStatus_6);
		int pendingInvestigationNum = auditWorkingAreaDao.queryProblemTaskListCount(map);

		map.put("caseStatus", AuditConstants.auditCaseStatus_4);
		int pendingAuditNum = auditWorkingAreaDao.queryCaseTaskListCount(map);

		result.put("investigatedNum", investigatedNum);
		result.put("distributionAuditNum", distributionAuditNum);
		result.put("pendingInvestigationNum", pendingInvestigationNum);
		result.put("pendingAuditNum", pendingAuditNum);

		return result;
	}

	@Override
	public Map<String, Object> queryProblemTaskList(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		int num = auditWorkingAreaDao.queryProblemTaskListCount(map);
		result.put("total", num);
		if (num > 0) {
			result.put("result", auditWorkingAreaDao.queryProblemTaskList(map));
		} else {
			result.put("result", null);
		}

		return result;
	}

	@Override
	public Map<String, Object> queryCaseTaskList(Map<String, Object> map) {
		Map<String, Object> result = new HashMap<String, Object>();
		int num = auditWorkingAreaDao.queryCaseTaskListCount(map);
		result.put("total", num);
		if (num > 0) {
			result.put("result", auditWorkingAreaDao.queryCaseTaskList(map));
		} else {
			result.put("result", null);
		}
		return result;
	}

	@Override
	public Map<String, Object> getQuestionCount(Map<String, Object> map) {

		Map<String, Object> result = new HashMap<String, Object>();
		// 获取当前月前12月，包含当前月
		List<String> dateList = getDateList();
		map.put("dateList", dateList);
		List<String> dateViewList = new ArrayList<String>();

		// 事件数据
		List<Map> resultNum = auditWorkingAreaDao.getQuestionCount(map);
		List<String> questionNum = new ArrayList<String>();
		for (int i = 0; i < dateList.size(); i++) {
			String dateTemp = dateList.get(i);
			String viewDate = dateTemp.substring(2, 4) + "-" + dateTemp.substring(4);
			for (int j = 0; j < resultNum.size(); j++) {
				Map tempMap = resultNum.get(j);
				String hearDate = (String) tempMap.get("hearDate");
				BigDecimal num = (BigDecimal) tempMap.get("num");
				if (dateTemp.equals(hearDate)) {
					questionNum.add(String.valueOf(num));
					break;
				}
				if (j == resultNum.size() - 1) {
					questionNum.add("0");
				}
			}
			dateViewList.add(viewDate);
		}
		result.put("dateList", dateViewList);
		result.put("investigatedData", questionNum);
		// 案件数据
		List<Map> resultCaseNum = auditWorkingAreaDao.getCaseCount(map);
		List<String> caseNum = new ArrayList<String>();
		for (int i = 0; i < dateList.size(); i++) {
			String dateTemp = dateList.get(i);
			for (int j = 0; j < resultCaseNum.size(); j++) {
				Map tempMap = resultCaseNum.get(j);
				String hearDate = (String) tempMap.get("hearDate");
				BigDecimal num = (BigDecimal) tempMap.get("num");
				if (dateTemp.equals(hearDate)) {
					caseNum.add(String.valueOf(num));
					break;
				}
				if (j == resultNum.size() - 1) {
					caseNum.add("0");
				}
			}
		}
		result.put("auditData", caseNum);
		return result;
	}

	private List<String> getDateList() {
		Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINESE);
		calendar.setTime(new Date());
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		List<String> dateList = new ArrayList<String>();
		String dateTemp = "";
		if (month < 10) {
			dateTemp = year + "0" + month;
		} else {
			dateTemp = year + "" + month;
		}

		dateList.add(dateTemp);
		for (int i = 1; i < 12; i++) {
			int tempMon = month - i;
			int tempYear = year;
			if (tempMon <= 0) {
				tempMon = tempMon + 12;
				tempYear = tempYear - 1;
			}
			if (tempMon < 10) {
				dateTemp = tempYear + "0" + tempMon;
			} else {
				dateTemp = tempYear + "" + tempMon;
			}

			dateList.add(dateTemp);
		}
		Collections.reverse(dateList);
		return dateList;
	}

	@Override
	public Map<String, Object> getQuestionProvider(Map<String, Object> map) {

		Map<String, Object> result = new HashMap<String, Object>();
		// 获取当前月前12月，包含当前月
		List<String> dateList = getDateList();
		map.put("dateList", dateList);
		List<String> numList = new ArrayList<String>();
		List<String> providerNameList = new ArrayList<String>();

		// 案件数据
		List<Map> resultNum = auditWorkingAreaDao.getQuestionProvider(map);
		// //补空
		// for (int i = 0; i < 10-resultNum.size(); i++) {
		// Map tempMap = new HashMap<String,Object>();
		// BigDecimal tempNum = new BigDecimal(0);
		// String tepmProviderName = "";
		// tempMap.put("num", tempNum);
		// tempMap.put("providerName", tepmProviderName);
		// resultNum.add(tempMap);
		//
		// }
		for (int i = 0; i < resultNum.size(); i++) {
			Map tempMap = resultNum.get(i);
			BigDecimal num = (BigDecimal) tempMap.get("num");
			numList.add(String.valueOf(num));
			String providerName = (String) tempMap.get("providerName");
			providerNameList.add(providerName);
		}
		result.put("numList", numList);
		result.put("providerNameList", providerNameList);
		return result;
	}

}
